<template>
  <div class="login">
    <div class="login_box">
      <!-- login -->
      <div class="login_logo">
        <div class="log"></div>
      </div>
      <!-- 表单 -->
      <div class="login_form">
        <!-- 手机号、验证码 -->
        <el-form :model="loginForm" :rules="loginRules" ref="loginForm" class="demo-loginForm">
          <el-form-item prop="mobile">
            <el-input v-model="loginForm.mobile"></el-input>
          </el-form-item>
          <el-form-item prop="code">
            <el-input v-model="loginForm.code"></el-input>
          </el-form-item>
          <el-form-item prop="checkFlag">
            <!-- 必选项 -->
            <el-checkbox v-model="loginForm.checkFlag">我已阅读并同意用户协议和隐私条款</el-checkbox>
          </el-form-item>
          <el-form-item>
            <!-- 登陆 -->
            <el-button
              v-model="loginForm.checkFlag"
              type="primary"
              class="login_bot"
              @click="doLoginCli"
            >登陆</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { login } from '../../api/user.js'
// 导入token封装
import { setToken } from '../../api/usertoken.js'
export default {
  name: 'Login',
  data () {
    return {
      loginForm: {
        mobile: '13911111111',
        code: '246810',
        checkFlag: false
      },
      loginRules: {
        mobile: [
          { required: true, message: '请输入手机号码', trigger: 'blur' },
          {
            validator: function (rule, value, callback) {
              if (/^1[34578]\d{9}$/.test(value) === false) {
                callback(new Error('请输入正确的手机号'))
              } else {
                callback()
              }
            },
            trigger: 'blur'
          }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'blur' },
          {
            validator: function (rule, value, callback) {
              if (!Number.isInteger(Number(value))) {
                callback(new Error('请输入数字类型验证码'))
              } else {
                callback()
              }
            },
            trigger: 'blur'
          },
          { min: 4, max: 6, message: '请输入4到6位验证码', trigger: 'blur' }
        ],
        checkFlag: [
          {
            validator: function (rule, value, callback) {
              if (value === false) {
                callback(new Error('请勾选同意协议'))
              } else {
                callback()
              }
            },
            trigger: 'change'
          }
        ]
      }
    }
  },
  methods: {
    async onLoginCli () {
      // 判断是否同意协议
      if (this.loginForm.checkFlag === false) return this.$message('请先同意协议')
      try {
        // 调登陆接口
        const { status, data: { data } } = await login(this.loginForm.mobile, this.loginForm.code)
        if (status !== 201) {
          return this.$message({
            type: 'warning',
            message: '登陆失败！'
          })
        }
        // 添加本地token
        setToken(data)
        this.$router.push('/')
      } catch (error) {
        console.log(error)
      }
    },
    doLoginCli () {
      // 判断form表单理数据的验证规则是否正确
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.onLoginCli()
        } else {
          this.$message({
            type: 'warning',
            message: '验证失败！'
          })
          return false
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login {
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  background: url("../../assets/img/login_bg.jpg") no-repeat;
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
  .login_box {
    min-width: 400px;
    padding: 30px;
    background: #fff;
    border-radius: 10px;
    box-shadow: 7px 8px 10px #222;
    .login_logo {
      margin-bottom: 20px;
      .log {
        width: 100%;
        height: 60px;
        background: url("../../assets/img/logo_index.png") no-repeat;
        background-position: center;
      }
    }
    .login_bot {
      width: 100%;
    }
  }
}
</style>
